package com.txx.app.main.modules.web.function;

import android.content.ComponentName;
import android.content.Intent;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.qihoo360.replugin.RePlugin;
import com.rsq.function.txxpluginsdk.plugin.TXXPluginManager;
import com.rsq.function.txxpluginsdk.plugin.interfaces.OnPluginOpenListener;
import com.txx.app.main.modules.web.WebCommonFragment;
import com.txx.app.main.modules.web.bean.BaseResult;
import com.txx.app.main.modules.web.js.H5BasePlugin;
import com.txx.app.main.modules.web.js.H5Event;

import org.json.JSONObject;

import java.util.Map;


/**
 * 加密的sdk
 */
public class JumpToNativeController extends H5BasePlugin {
    private String TAG = JumpToNativeController.class.getSimpleName ();

    public JumpToNativeController() {
    }

    @Override
    public boolean handleEvent(H5Event h5Event, final WebCommonFragment fragment) {
        if (h5Event == null) {
            return false;
        }
        try {
            String json = h5Event.getJsonSource ();
            // 解析H5页面的入参
            JSONObject jsonObject = new JSONObject (json);
            String pluginName = jsonObject.optString ("pluginName");
            String className = jsonObject.optString ("className");
            String params = jsonObject.optString ("params");
            final String callBackName = jsonObject.optString ("callBackName");
            if ("replugin-host".equals (pluginName)) {
//                新加的内容 未测试
//                跳转宿主
                Intent intent = new Intent ();
                intent.setComponent (new ComponentName ("com.taikanglife.isalessystem", className));
                Map<String, Object> paramsMap = getResultMap (params);
                if(paramsMap!=null) {
                    for (String key : paramsMap.keySet()) {
                        Object value = paramsMap.get(key);
                        if (value instanceof Boolean) {
                            intent.putExtra(key, (Boolean) value);
                        } else if (value instanceof Integer) {
                            intent.putExtra(key, (Integer) value);
                        } else if (value instanceof Double) {
                            intent.putExtra(key, (Double) value);
                        } else if (value instanceof Float) {
                            intent.putExtra(key, (Float) value);
                        } else {
                            intent.putExtra(key, (String) value);
                        }
                    }
                }
                fragment.startActivity (intent);
//                跳转宿主默认都成功
                BaseResult result = new BaseResult ("0", "跳转成功");
                //        处理后调用
                if (fragment != null) {
                    fragment.getH5CallBackCommon (callBackName, new Gson ().toJson (result));
                }
            } else {
//                跳转插件
                Intent intent = RePlugin.createIntent (pluginName, className);
                Map<String, Object> paramsMap = getResultMap (params);
                if(paramsMap!=null) {
                    for (String key : paramsMap.keySet()) {
                        Object value = paramsMap.get(key);
                        if (value instanceof Boolean) {
                            intent.putExtra(key, (Boolean) value);
                        } else if (value instanceof Integer) {
                            intent.putExtra(key, (Integer) value);
                        } else if (value instanceof Double) {
                            intent.putExtra(key, (Double) value);
                        } else if (value instanceof Float) {
                            intent.putExtra(key, (Float) value);
                        } else {
                            intent.putExtra(key, (String) value);
                        }
                    }
                }
                TXXPluginManager.getInstance ().startActivity (fragment.getContext (), pluginName, intent, new OnPluginOpenListener () {
                    @Override
                    public void onSuccess() {
                        BaseResult result = new BaseResult ("0", "跳转成功");
                        //        处理后调用
                        if (fragment != null) {
                            fragment.getH5CallBackCommon (callBackName, new Gson ().toJson (result));
                        }
                    }

                    @Override
                    public void onFailure() {
                        BaseResult result = new BaseResult ("-100", "跳转失败");
                        //        处理后调用
                        if (fragment != null) {
                            fragment.getH5CallBackCommon (callBackName, new Gson ().toJson (result));
                        }
                    }
                });

            }


        } catch (Exception e) {
            e.printStackTrace ();
        }
        return true;
    }

    @Override
    public void onRelease() {

    }


    //JSON字符串转Map
    protected Map getResultMap(String JSONString) {
        if (TextUtils.isEmpty (JSONString)) {
            return null;
        }
        Map t = null;
        try {
            Gson gson = new Gson ();
            t = gson.fromJson (JSONString, Map.class);
        } catch (Exception e) {
            e.printStackTrace ();
        }
        return t;
    }

}
